package com.cyber.parkingremotev2;

import java.util.ArrayList;


public class CommandSystem {
	// parameter type
	
	public static final int CS_MODE = 1;
	public static final int CS_DIRECTION = 2;
	public static final int CS_DISTANCE = 3;
	public static final int CS_SPEED = 4;
	public static final int CS_TRIG_MIN = 5;
	public static final int CS_EXTENDED = 6;

	public static final int CS_EMPTY = 0;
	public static final int CS_FORWARD = 1;
	public static final int CS_BACKWARD = 2;
	public static final int CS_AUTO_FORWARD = 3;
	public static final int CS_AUTO_BACKWARD = 4;
	public static final int CS_STOP = 5;
	public static final int CS_MILESTONE = 6;


	public static final int CS_PAUSE = 0;
	public static final int CS_MOVE = 1;
	public static final int CS_RESTART = 2;
	public static final int CS_LOOP = 3;
	public static final int CS_JUMP = 4;
	public static final int CS_STATUS = 5;
	public static final int CS_REQUIRE = 6;
	public static final int CS_SAVE = 7;
	public static final int CS_CONTINUE = 8;
	
	public static final int CS_STATUS_OFFSET = 100;



	public static final int CS_MAX_COMMAND = 25;
	public static final int CS_INVALID_VALUE = -1;

	private static final String CS_FS = ":";
	private static final String CS_CS = "#";
	

	public String encode(int seq, int type, int value) {
		StringBuilder sb = new StringBuilder();
		sb.append(seq);
		sb.append(CS_FS);
		sb.append(type);
		sb.append(CS_FS);
		sb.append(value);
		sb.append(CS_CS);
		return sb.toString();
	}

	public String getParameter(int seq, int... parameters) {
		StringBuilder sb = new StringBuilder();
		for (int i = 1; i <= parameters.length; ++i) {
			sb.append(encode(seq, i, parameters[i - 1]));
		}
		return sb.toString();
	}
	public String getParameter(int seq, ArrayList<Integer> parameters) {
		StringBuilder sb = new StringBuilder();
		for (int i = 1; i <= parameters.size(); ++i) {
			sb.append(encode(seq, i, parameters.get(i - 1)));
		}
		return sb.toString();
	}

	public String getCommand(int command, int argument) {
		return encode(0, command, argument);
	}

	public ArrayList<Command> decode(String data) {
		String[] info_data = data.split("#");
		ArrayList<Command> rst = new ArrayList<Command>();
		for(String info:info_data){
			Command c = decodeOne(info);
			if (c != null){
				rst.add(c);
			}
		}
		return rst;
	}

	public Command decodeOne(String data) {
		String[] info = data.split(":");
		if (info.length == 3) {
			Command rst = new Command(Integer.valueOf(info[0]),
					Integer.valueOf(info[1]), Integer.valueOf(info[2]));
			return rst;
		}
		return null;
	}
	
	//just for convenient
	public String move(){
		return getCommand(CS_MOVE, 0);
	}
	
	public String pause(){
		return getCommand(CS_PAUSE, 0);
	}
	
	public String restart(){
		return getCommand(CS_RESTART, 0);
	}
	
	public String nextStep(){
		return getCommand(CS_CONTINUE, 0);
	}
	
	public String status(){
		return getCommand(CS_STATUS, 0);
	}
	
	public String require(int seq){
		return getCommand(CS_REQUIRE, seq);
	}
	
	public String loop(boolean state){
		if (state){
			return getCommand(CS_LOOP, 1);
		}else{
			return getCommand(CS_LOOP, 0);
		}
	}
	
	public String save(){
		return getCommand(CS_SAVE, 0);
	}
	


	public class Command {
		public Command(int seq, int type, int value) {
			this.seq = seq;
			this.type = type;
			this.value = value;
		}
	
		public int seq;
		public int type;
		public int value;
	}

}
